package com.cnhis.cloudhealth.clinical.hosnurse.hosdeladvice.bo;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosnurse.hosdeladvice.dao.HosDelAdviceDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.JsonUtil;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;

import net.sf.json.JSONArray;

@Service
public class HosDelAdviceBo {
  @Resource
  private HosDelAdviceDao hosDelAdviceDao;
  @Resource
   private GetNewId getNewId;
  private int getVAF59(Map map)throws Exception{
	  return hosDelAdviceDao.getVAF59(map);
  }
  @SuppressWarnings("unchecked")
public ModelVo  delAdviceList(Map map)throws Exception{
	  ModelVo mo = new ModelVo();
		List ls =null;
		try {
			if("2".equals(map.get("lVAF04").toString())){
				map.put("VAF59", getVAF59(map));
			}else{
				map.put("VAF59", 0);
			}
			ls = hosDelAdviceDao.delAdviceList(map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
			return mo;
		}
		mo.setList(ls);
		mo.setResult(StaticKeys.OH_SUCCESS);
		mo.setRows(ls);
		return mo;
  }
  public ModelVo checkCASign(Map map)throws Exception{
	  ModelVo mo = new ModelVo();
			List ls =null;
			try {
				ls = hosDelAdviceDao.checkCASign(map);
				
			} catch (Exception e) {
				e.printStackTrace();
				mo.setResult(StaticKeys.OH_FALSE);
				return mo;
			}
			mo.setList(ls);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setRows(ls);
			return mo;
  }
  private List getTempVaf(List vaf1)throws Exception{
	  return hosDelAdviceDao.getTempVaf(vaf1);
  }
  @SuppressWarnings("unchecked")
private void delVafRefernce(Map map)throws Exception{
	  map.put("VBG01", getNewId.nextId());
	  map.put("VBG01a", getNewId.nextId());
	  hosDelAdviceDao.delVafRefernce(map);
  }
  public ModelVo delclinicAdvice(Map map) throws Exception {
		// 获取业务分类编号
		Integer lacf01 = Integer.valueOf(map.get("lacf01").toString());
		ModelVo mo = new ModelVo();
		// 住院业务
		if (lacf01.equals(2)) {
			String json = map.get("lXml").toString();
			List list = JsonUtil.getList(JSONArray.fromObject(json).toString());
			//待删除医嘱信息
			List vaf2 = getTempVaf(list);
			
			if(vaf2==null||vaf2.size()==0){
				mo.setResult(StaticKeys.OH_FALSE);
				mo.setResultMsg("医嘱都已执行，没有可以删除的医嘱");
				return mo;
			}
			//删除医嘱单，病修改先关表 增加医嘱操作日志
			map.put("list", vaf2);
			delVafRefernce(map);
			
		}
		return mo;
	}
}
